package com.coscon.service.patternCommon.impl.systemos;

import com.coscon.model.IrcaCmd;
import com.coscon.service.patternCommon.ICommand;
import lombok.extern.slf4j.Slf4j;

import javax.websocket.Session;
import java.awt.*;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.io.IOException;

/**
 * 系统控制：关机
 */
@Slf4j
public class SystemOsPowerOffCmdImpl implements ICommand {

    private Robot rb;

    public SystemOsPowerOffCmdImpl(Robot rb){
        this.rb = rb;
    }



    @Override
    public Boolean executeByMac(IrcaCmd cmd, Session session) {
        execShutdownByMac();
        return true;
    }

    @Override
    public Boolean executeByWin(IrcaCmd cmd, Session session) {
        this.execShutdownByWin();
        return true;
    }

    @Override
    public Boolean executeByLinux(IrcaCmd cmd, Session session) {
        return true;
    }


    public void execShutdownByMac() {
        rb.mouseMove(100, 0); //鼠标指针移到最顶部停留
        rb.delay(1000);

        rb.mouseMove(28, 15); //鼠标指针移到左上角的苹果图标
        rb.delay(100);
        //执行单击
        rb.mousePress(InputEvent.BUTTON1_MASK);
        rb.mouseRelease(InputEvent.BUTTON1_MASK);
        rb.delay(100);

        rb.mouseMove(28, 240); //鼠标指针移到关机菜单的坐标
        rb.delay(100);

        //快捷关机键:Shift + Ctrl + Opt + 关机:瞬间关机。
        rb.keyPress(KeyEvent.VK_SHIFT);
        rb.keyPress(KeyEvent.VK_CONTROL);
        rb.keyPress(KeyEvent.VK_ALT);
        //执行单击"关机"
        rb.mousePress(InputEvent.BUTTON1_MASK);
        rb.mouseRelease(InputEvent.BUTTON1_MASK);
        rb.delay(100);

        System.exit(0);
    }

    public void execShutdownByWin() {
        try {
            Runtime.getRuntime().exec("cmd /c shutdown -s -t 0");
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
        }
    }

}
